<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <view>
        <view class="name titleBold">{{ info.name }}<span v-if="info.noRequire">(非必填)</span></view>
        <view v-if="info.tips" class="tips"> {{ info.tips }} </view>
        <view class="sickList">
          <view
            v-for="item in info.options"
            :key="item.id"
            :class="[info.rowBlock === true ? 'sickBox blocks' : 'sickBox']"
          >
            <view :class="[checkValue === item.id ? 'sickSpan cur' : 'sickSpan']" @click="choseList(item)">
              {{ item.name }}
              <span v-if="checkValue === item.id" class="curImg"><img :src="curIcon" /></span>
            </view>
            <view v-if="item.tips" class="kidTips">{{ item.tips }}</view>
          </view>
        </view>
      </view>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    // 疾病大类title
    sickTitleCom
  },
  props: {
    // 是否展示搜索按钮
    info: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      checkValue: undefined,
      curIcon: curIcon,
      noChose: false
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    },
    uSex() {
      return this.$store.getters.userSex;
    }
  },
  methods: {
    async nextBtn() {
      // 疾病直接提交接口
      let submitData = this.assessForm;
      if (!this.checkValue && this.checkValue !== 0 && !this.info.noRequire) {
        uni.showToast({ title: `请选择${this.info.name}`, icon: 'none' });
        return false;
      }
      submitData[this.info.key] = this.checkValue;
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      const ajaxData = {
        [this.info.key]: this.checkValue
      };
      let healthData = await ajaxSend(ajaxData);
      let curActive = 0;
      if (this.uSex && this.uSex === '男' && this.active === 6) {
        curActive = this.active + 2;
      } else {
        curActive = this.active + 1;
      }

      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      } else {
        this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
    },
    proBtn() {
      let curActive = 0;
      if (this.uSex && this.uSex === '男' && this.active === 8) {
        curActive = this.active - 2;
      } else {
        curActive = this.active - 1;
      }
      // curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    choseList(val) {
      this.checkValue = val.id;
    }
  }
};
</script>
<style lang="scss">
.comBox {
  background-color: #fff;
  .title {
    background: rgba(216, 246, 214, 1);
    color: rgba(0, 0, 0, 0.55);
    font-size: 32rpx;
    padding: 14rpx;
    text-align: center;
  }
  .body {
    padding: 24rpx;
    .name {
      padding: 20rpx 0;
    }
    .tips {
      color: rgba(0, 0, 0, 0.55);
      font-size: 28rpx;
      margin-bottom: 20rpx;
      padding: 0 16rpx;
    }
    .sickList {
      .sickBox {
        display: inline-block;
        width: 50%;
        .sickSpan {
          display: inline-block;
          text-align: center;
          width: 80%;
          padding: 10rpx 18rpx;
          margin: 12rpx 16rpx;
          border: 2rpx solid rgba(178, 178, 178, 1);
          border-radius: 16rpx;
          font-size: 28rpx;
          position: relative;
          .curImg {
            position: absolute;
            width: 40rpx;
            height: 40rpx;
            right: 0;
            bottom: 0;
            border: none;
            img {
              width: 40rpx;
              height: 40rpx;
            }
          }
        }
        .cur {
          border: 2rpx solid rgba(242, 85, 89, 1);
          color: rgba(242, 85, 89, 1);
        }
        .kidTips {
          color: rgba(0, 0, 0, 0.55);
          font-size: 28rpx;
          margin-bottom: 30rpx;
          padding: 0 16rpx;
        }
      }
      .blocks {
        display: block;
        width: 100%;
        .sickSpan {
          width: 590rpx;
        }
      }
    }
  }
}
.clear {
  clear: both;
  overflow: hidden;
  zoom: 1;
  height: 0px;
}
.lineHeight86 {
  height: 86rpx;
  line-height: 86rpx;
}
.size28 {
  font-size: 28rpx;
}
.titleBold {
  font-weight: 500;
}
</style>
